package main

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"math/rand"
	"plg-log/core"
	"plg-log/global"
	"strconv"
	"time"
)

//type LogUser struct {
//	Id            int64  `json:"id" form:"id"`
//	Name          string `json:"name" form:"name"`
//	Age           int    `json:"age" form:"age"`
//	Address       string `json:"address" form:"address"`
//	Number        string `json:"number" form:"number"`
//	Email         string `json:"email" form:"email"`
//	DetailAddress string `json:"detail_address" form:"detail_address"`
//	IsDelete      int    `json:"is_delete" form:"is_delete"`
//	Gender        int    `json:"gender" form:"gender"`
//	BirthDate     string `json:"birth_date" form:"birth_date"`
//}

func main() {
	v := viper.New()
	v.SetConfigFile("config.yaml")
	v.SetConfigType("yaml")
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
	v.WatchConfig()
	v.OnConfigChange(func(e fsnotify.Event) {
		fmt.Println("config file changed:", e.Name)
		if err = v.Unmarshal(&global.PLG_CONFIG); err != nil {
			fmt.Println(err)
		}
	})
	if err = v.Unmarshal(&global.PLG_CONFIG); err != nil {
		fmt.Println(err)
	}
	global.ZapLogger = core.Zap()

	router := gin.Default()

	for i := 1; i <= 1000; i++ {
		user := generateRandomUser(int64(i))
		global.UserLogMap[int64(i)] = &user
	}

	router.POST("/info", func(c *gin.Context) {
		// 生成1000条日志记录
		for i := 1; i <= 10; i++ {
			randInt := randomInt(30)
			action := global.ActionsMap[randInt]
			randUserInt := randomInt(1000)
			user := global.UserLogMap[int64(randUserInt)]
			global.ZapLogger.Info(action, zap.Any("user", *user))
		}
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "ok",
		})
	})
	router.POST("/err", func(c *gin.Context) {
		// 生成1000条日志记录
		for i := 1; i <= 10; i++ {
			randInt := randomInt(30)
			action := global.ErrorMap[randInt]
			randUserInt := randomInt(1000)
			user := global.UserLogMap[int64(randUserInt)]
			global.ZapLogger.Error(action, zap.Any("user", *user))
		}
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "ok",
		})
	})
	router.Run(":8888")
}

func randomInt(intMax int) int {
	rand.Seed(time.Now().UnixNano())
	return rand.Intn(intMax) + 1
}

func generateRandomUser(i int64) global.LogUser {
	// 设定随机数种子
	rand.Seed(time.Now().UnixNano())

	// 随机生成Name（user + 随机数）
	name := "user" + strconv.Itoa(rand.Intn(10000))

	// 随机生成Age（例如：18到60之间）
	age := rand.Intn(43) + 18

	// 随机生成Address（address + 随机数）
	address := "address" + strconv.Itoa(rand.Intn(10000))

	// 随机生成Number（number + 随机数）
	number := "number" + strconv.Itoa(rand.Intn(10000))

	// 随机生成Email（email + 随机数 + @gmail.com）
	email := "email" + strconv.Itoa(rand.Intn(10000)) + "@gmail.com"

	// 随机生成DetailAddress（detail_address + 随机数）
	detailAddress := "detail_address" + strconv.Itoa(rand.Intn(10000))

	// 随机生成IsDelete（0 或 1）
	isDelete := rand.Intn(2)

	// 随机生成Gender（1 或 2，代表男女）
	gender := rand.Intn(2) + 1

	// 随机生成BirthDate（例如：生成 1990-01-01 到 2000-12-31 的日期）
	year := rand.Intn(11) + 1990 // 1990到2000之间的随机年份
	month := rand.Intn(12) + 1   // 1到12之间的随机月份
	day := rand.Intn(28) + 1     // 1到28之间的随机日期（为了简化处理）

	birthDate := fmt.Sprintf("%d-%02d-%02d", year, month, day)

	// 返回生成的 LogUser 对象
	return global.LogUser{
		Id:            i,
		Name:          name,
		Age:           age,
		Address:       address,
		Number:        number,
		Email:         email,
		DetailAddress: detailAddress,
		IsDelete:      isDelete,
		Gender:        gender,
		BirthDate:     birthDate,
	}
}
